home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5393 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  58 lines

  1. Path: gidora.kralizec.net.au!root
  2. From: rosko@zeta.org.au (Ross McKay)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Code Base 5.1 problem
  5. Date: Sun, 04 Feb 1996 09:53:13 GMT
  6. Organization: Soft Technologies
  7. Message-ID: <4f1sib$2l1@gidora.kralizec.net.au>
  8. References: <4eufsf$908@granite.sentex.net>
  9. Reply-To: rosko@zeta.org.au
  10. NNTP-Posting-Host: dialup48.syd1.zeta.org.au
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. mvejvoda@sentex.net (Mark Vejvoda) wrote:
  14.  
  15. >I am writing my own DLL and I'm tryin to link in a codebase 5.1 static
  16. >library (as per instructions in the COMPILER.DOC) in Borlandc C++
  17. >4.02. In my D4ALL.H header i've declated some global vars to be used
  18. >in each .C module that i write. When i include d4all.h in these .C
  19. >modules the compile works.. but the linker says that the global
  20. >variables have been duplicated in each subsequent .C module. Why is
  21. >this.. what have i done wrong?
  22.  
  23. Hi Mark, what you have done by "declaring" your global variables in
  24. your .h file is actually to *define* them there.
  25.  
  26. The difference between declaring and defining globals is that
  27. declaring tells the compiler that they exist, and what type they are,
  28. whereas defining them tells the compiler to "create an instance" of
  29. them.
  30.  
  31. e.g. of declaring (this tells compiler there WILL BE a foobar):
  32.  
  33.    extern int foobar;
  34.  
  35. e.g. of defining (this tells compiler to MAKE a foobar):
  36.  
  37.    int foobar;
  38.  
  39. You should declare your global variables ONCE in your .h file, and
  40. then all source files that include this .h file will know about your
  41. global variables.
  42.  
  43. You should define your global variables ONCE in ONLY ONE of your
  44. source files, i.e. one .c or .cpp (or .cxx or .C or .foobar ;-)
  45. It is quite common (even recommended) practice to define all globals
  46. most related to a module in that module, or even have one source file
  47. specifically for globals (e.g. global.c)
  48.  
  49. Regards,
  50. ------------------------------------------------------------------
  51. Ross McKay        | snail: GPO Box 562, Sydney NSW 2001 Australia
  52. Soft Technologies | email: mailto:rosko@zeta.org.au
  53. Sydney, Australia |   URL: http://www.zeta.org.au/~rosko
  54. ------------------------------------------------------------------
  55. The opinions expressed are my own, not those of Soft Technologies.
  56.     "The beatings will continue, until staff morale improves."
  57.  
  58.